package yogiro.controller;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import yogiro.service.ReservationService;
import yogiro.to.ProductTO;

//controller
public class ReservationController extends MultiActionController{
	
	private ReservationService service;
	
	public ReservationController(ReservationService service){
		this.service = service;
	}
  
		//상품 등록
		public ModelAndView registerProduct(HttpServletRequest request,HttpServletResponse response, ProductTO pto) throws Exception{
			
			String dir = getServletContext().getRealPath("/reservation/photo");//사진을 저장할 경로 지정
			List list = pto.getUpFile();//사진정보 업로드
			ArrayList fileNames = new ArrayList();
			for(int i = 0 ; i<list.size();i++){
				MultipartFile mFile = (MultipartFile)list.get(i);
				if(!mFile.isEmpty()){
					String fileName = mFile.getOriginalFilename();
					File destination = new File(dir,fileName);
					mFile.transferTo(destination);
					fileNames.add(fileName);
				}
				
			}
			pto.setPhotoName(fileNames);

			//비지니스 로직 - 상품 등록
			service.registerProduct(pto);
			//응답
			return new ModelAndView("reservation/product_success_show","pto",pto);
		}
		public ModelAndView listAllProduct(HttpServletRequest request, HttpServletResponse response) throws SQLException{
			//BL
			ArrayList list = service.listAllProduct();
			
			//응답
			return new ModelAndView("reservation/product_list","list",list);
		}
		public ModelAndView showProduct(HttpServletRequest request, HttpServletResponse response) throws SQLException{
			int no = Integer.parseInt(request.getParameter("no"));
			HashMap map = service.productByNo(no);
			return new ModelAndView("reservation/product_show",map);
		}
//		//상품 수정
//		public MedelAndView ShowModifyProduct(HttpServletRequest request,HttpServletResponse reponse, ProductTO pto)throws SQLException{
//			
//			return new ModelAndview("product_show","pto",pto);
//		}
//		//상품 리스트 출력

		//상품 정보 수정
//		public ModelAndView modifyTourProduct(HttpServletRequest request, HttpServletResponse response) throws SQLExcepion{
//			//요청 파리미터
//			
//			
//		}
}
 